#include "stdafx.h"
#include "FocusManager.h"
#include "View.h"
#include "WidgetWin.h"
#include "RootView.h"

FocusManager::FocusManager()
	: m_hoverView(NULL)
	, m_focusView(NULL)
{
}


FocusManager::~FocusManager()
{
}

View*& FocusManager::getHoverView()
{
	return m_hoverView;
}

View*& FocusManager::getFocusView()
{
	return m_focusView;
}


bool FocusManager::onMouseMove(short x, short y)
{
	// #. Find the top view.
	RootView* rootView = WidgetWin::getInstance().getRootView();
	if (rootView == NULL)
	{
		return false;
	}

	View* view = rootView->FindTopViewInPoint(x, y);
	// #. handle last hover view status
	View* lastHoverView = m_hoverView;

	if (view != lastHoverView)
	{
		if (lastHoverView)
		{
			lastHoverView->mouseLeave();
		}

		if (view && view != rootView)
		{
			view->mouseEntry();
		}
		m_hoverView = view;
	}

	// #. handle current view

	return true;
}

bool FocusManager::onLButtonDown(short x, short y)
{
	RootView* rootView = WidgetWin::getInstance().getRootView();
	if (rootView == NULL)
	{
		return false;
	}

	View* view = rootView->FindTopViewInPoint(x, y);

	// #. last view.
	if (view != m_focusView)
	{
		if (m_focusView)
		{
			m_focusView->setStatus(View::STATUS_NORMAL);
			m_focusView->loseFocus();
		}
	}

	// #. current view
	if (view)
	{
		view->setStatus(View::STATUS_FOCUS);
		m_focusView = view;
		m_focusView->gotFocus();
	}

	return true;
}

bool FocusManager::onLButtonUp(short x, short y)
{

	// onMouseMove(x, y);
	return true;
}
